package com.dyzs.client;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.dyzs.util.Constants;
import com.dyzs.util.WSUtils;

public class MoneyClient {

//更新报账详情
	public String getBZDetail(String planId,String type) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", planId);
		params.put("arg1", type);
		try {
			String resultXml = WSUtils.callWS("getBZDetail", params,Constants.PlanURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
//	服务器报账
	public String BZ(String str,String type) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", str);
		params.put("arg1", type);
		try {
			String resultXml = WSUtils.callWS("BZ", params,Constants.PlanURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	//酒店 解析
	public List<Map<String, Object>> pullBZHotelDetailXml(String resultXml) throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			List list = null;			
			Map<String, Object> map = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						list=new ArrayList();						
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							map=new HashMap();
						}else if ("id".equals(parser.getName())) {
							map.put("id", parser.nextText());
						}else if ("name".equals(parser.getName())) {
							map.put("name", parser.nextText());
						} else if ("d".equals(parser.getName())) {
							map.put("date", parser.nextText());
						}else if ("datenum".equals(parser.getName())) {
							map.put("dateNum", parser.nextText());
						}else if ("total".equals(parser.getName())){
							map.put("total", parser.nextText());
						}else if ("type".equals(parser.getName())) {
							map.put("type", parser.nextText());
						}else if ("sfje".equals(parser.getName())) {
							map.put("sfje", parser.nextText());							
						}else if ("remark".equals(parser.getName())) {
							map.put("remark", parser.nextText());
						}						
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {							
							list.add(map);
							map=null;						
						}
						break;
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！");
			}
			return list;
		}
	}
	
	
	
	//用餐解析	
	public List<Map<String, Object>> pullBZEatingDetailXml(String resultXml) throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			List list = null;			
			Map<String, Object> map = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						list=new ArrayList();						
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							map=new HashMap();
						}else if ("id".equals(parser.getName())) {
							map.put("id", parser.nextText());
						}else if ("ctmc".equals(parser.getName())) {
							map.put("ctmc", parser.nextText());
						} else if ("ycsj".equals(parser.getName())) {
							map.put("ycsj", parser.nextText());
						}else if ("lx".equals(parser.getName())) {
							map.put("lx", parser.nextText());
						}else if ("cb".equals(parser.getName())){
							map.put("cb", parser.nextText());
						}else if ("rs".equals(parser.getName())) {
							map.put("rs", parser.nextText());
						}else if ("mc".equals(parser.getName())) {
							map.put("mc", parser.nextText());
						}else if ("sfje".equals(parser.getName())) {
							map.put("sfje", parser.nextText());								
						}else if ("bz".equals(parser.getName())) {
							map.put("bz", parser.nextText());
						}else if ("zflx".equals(parser.getName())) {
							map.put("zflx", parser.nextText());							
						}else if ("total".equals(parser.getName())) {
							map.put("total", parser.nextText());
							
						}							
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {							
							list.add(map);
							map=null;						
						}
						break;
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！");
			}
			return list;
		}
	}
	
	
	
	
	
	
	//用车解析	
	public List<Map<String, Object>> pullBZCarDetailXml(String resultXml) throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			List list = null;			
			Map<String, Object> map = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						list=new ArrayList();						
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							map=new HashMap();
						}else if ("id".equals(parser.getName())) {
							map.put("id", parser.nextText());
						}else if ("ycq".equals(parser.getName())) {
							map.put("ycq", parser.nextText());
						} else if ("ycz".equals(parser.getName())) {
							map.put("ycz", parser.nextText());
						}else if ("cj".equals(parser.getName())) {
							map.put("cj", parser.nextText());
						}else if ("zflx".equals(parser.getName())){
							map.put("zflx", parser.nextText());
						}else if ("bt".equals(parser.getName())) {
							map.put("bt", parser.nextText());
						}else if ("lqf".equals(parser.getName())) {
							map.put("lqf", parser.nextText());
						}else if ("sfje".equals(parser.getName())) {
							map.put("sfje", parser.nextText());								
						}else if ("bz".equals(parser.getName())) {
							map.put("bz", parser.nextText());								
						}else if ("total".equals(parser.getName())) {
							map.put("total", parser.nextText());								
						}
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {							
							list.add(map);
							map=null;						
						}
						break;
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！");
			}
			return list;
		}
	}
	
	
	
	
	
	
	//景点解析	
	public List<Map<String, Object>> pullBZSignDetailXml(String resultXml) throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			List list = null;			
			Map<String, Object> map = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						list=new ArrayList();						
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							map=new HashMap();
						}else 
						if(map!=null && !("root".equals(parser.getName())))
							map.put(parser.getName(),parser.nextText());
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {							
							list.add(map);
							map=null;						
						}
						break;
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！");
			}
			return list;
		}
	}
	
	
//	获取记账列表
	public String getJZ(String userId, String date) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", userId);
		params.put("arg1", date);
		try {
			String resultXml = WSUtils.callWS("getChargeBook", params,Constants.ChargeURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	
	public String deleteJZ(String ids) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", ids);
		try {
			String resultXml = WSUtils.callWS("delChargeBook", params,Constants.ChargeURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	
//	解析记账列表
	public List<Map<String, Object>> pullJZXml(String resultXml) throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			List list = null;			
			Map<String, Object> map = null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_DOCUMENT:
						list=new ArrayList();						
						break;
					case XmlPullParser.START_TAG:
						if ("item".equals(parser.getName())) {
							map=new HashMap();
						}else if ("id".equals(parser.getName())) {
							map.put("id", parser.nextText());
						}else if ("type".equals(parser.getName())) {
							map.put("type", parser.nextText().equals("0")?"支出":"收入");
						} else if ("content".equals(parser.getName())) {
							map.put("content", parser.nextText());
						}else if ("money".equals(parser.getName())) {
							map.put("money", parser.nextText());
						}
						break;
					case XmlPullParser.END_TAG:
						if ("item".equals(parser.getName())) {							
							list.add(map);
							map=null;						
						}
						break;
					}
					event = parser.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！");
			}
			return list;
		}
	}
	
	
	
//	服务器记账
	public String JZ(String id,String userId, String type,String money,String content,String date) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", id);
		params.put("arg1", userId);
		params.put("arg2", type);
		params.put("arg3", money);
		params.put("arg4", content);
		params.put("arg5", date);
		try {
			String resultXml = WSUtils.callWS("saveChargeBook", params,Constants.ChargeURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

}
